using System;
using System.Linq;
using System.Xml.Linq;
using System.Web.UI;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;

using DesertedRoadStudios.BlogNetNuke.Common;
using DesertedRoadStudios.BlogNetNuke.Components;

namespace DesertedRoadStudios.BlogNetNuke
{
    public partial class Main : PortalModuleBase, IActionable
    {
        private XDocument _doc;

        protected void Page_Init(object sender, EventArgs e)
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            bnn.UpdateTabID(PortalId, ModuleId, TabId);

            _doc = Methods.GetParams(this.Context, PortalId, ModuleId, TabId);
            Session["BlogNetNuke"] = _doc;

            //Controls.Add(new LiteralControl("<strong style=\"color:#f00;\">" + _doc.Element("params").Element("RequestType").Value  + "</strong><br /><br />"));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            object[] parameters = { PortalId, ModuleId, TabId, _doc };
            if (_doc.Element("params").Element("Settings").Elements().Where(d => d.Name == "BlogViewContainerControl").Any() &&
                !string.IsNullOrEmpty(_doc.Element("params").Element("Settings").Element("BlogViewContainerControl").Value))
            {
                UserControl control = Page.LoadControl(_doc.Element("params").Element("Settings").Element("BlogViewContainerControl").Value, parameters);
                Controls.Add(control);
            }
            else
            {
                Controls.Add(new LiteralControl("<strong style=\"color:#f00;\">Module configuration settings have not been made.  Please configure module.</strong><br /><br />"));
            }
        }

        #region IActionable Members

        public ModuleActionCollection ModuleActions
        {
            get
            {
                
                ModuleActionCollection actions = new ModuleActionCollection();

                if (_doc.Element("params").Element("RequestType").Value.Equals(RequestType.PostView.ToString()))
                {
                    Actions.Add(this.GetNextActionID(), "Edit Post", ModuleActionType.AddContent, "", "~/DesktopModules/BlogNetNuke/images/edit.png", this.EditUrl("postID", _doc.Element("params").Element("Post").Element("PostDetails").Element("PostID").Value, "compose"), false, DotNetNuke.Security.SecurityAccessLevel.Edit, true, false);
                    Actions.Add(this.GetNextActionID(), "Delete Post", ModuleActionType.AddContent, "", "~/DesktopModules/BlogNetNuke/images/delete.png", this.EditUrl("deletePostID", _doc.Element("params").Element("Post").Element("PostDetails").Element("PostID").Value, "compose"), false, DotNetNuke.Security.SecurityAccessLevel.Edit, true, false);
                }

                Actions.Add(this.GetNextActionID(), "Compose New Post", ModuleActionType.AddContent, "", "~/DesktopModules/BlogNetNuke/images/add.png", this.EditUrl("", "", "compose"), false, DotNetNuke.Security.SecurityAccessLevel.Edit, true, false);

                return actions;
            }
        }

        #endregion
    }
}